home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / ghostscript / src / zdict.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  7KB  |  292 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zdict.c */
  20. /* Dictionary operators for Ghostscript */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "dict.h"
  25. #include "dstack.h"
  26. #include "iname.h"            /* for dict_find_name */
  27. #include "store.h"
  28.  
  29. /* <int> dict <dict> */
  30. int
  31. zdict(register os_ptr op)
  32. {    check_type(*op, t_integer);
  33.     if ( op->value.intval < 0 || op->value.intval > dict_max_size )
  34.         return_error(e_rangecheck);
  35.     return dict_create((uint)op->value.intval, op);
  36. }
  37.  
  38. /* <dict> maxlength <int> */
  39. int
  40. zmaxlength(register os_ptr op)
  41. {    check_type(*op, t_dictionary);
  42.     check_dict_read(*op);
  43.     make_int(op, dict_maxlength(op));
  44.     return 0;
  45. }
  46.  
  47. /* <dict> <int> .setmaxlength - */
  48. int
  49. zsetmaxlength(register os_ptr op)
  50. {    uint new_size;
  51.     int code;
  52.     os_ptr op1 = op - 1;
  53.     check_type(*op1, t_dictionary);
  54.     check_dict_write(*op1);
  55.     check_type(*op, t_integer);
  56.     if ( op->value.intval < 0 || op->value.intval > dict_max_size )
  57.         return_error(e_rangecheck);
  58.     new_size = (uint)op->value.intval;
  59.     if ( dict_length(op - 1) > new_size )
  60.         return_error(e_dictfull);
  61.     code = dict_resize(op - 1, new_size);
  62.     if ( code >= 0 ) pop(2);
  63.     return code;
  64. }
  65.  
  66. /* <dict> begin - */
  67. int
  68. zbegin(register os_ptr op)
  69. {    check_type(*op, t_dictionary);
  70.     check_dict_read(*op);
  71.     if ( dsp == dstop )
  72.         return_error(e_dictstackoverflow);
  73.     ++dsp;
  74.     ref_assign(dsp, op);
  75.     pop(1);
  76.     return 0;
  77. }
  78.  
  79. /* - end - */
  80. int
  81. zend(register os_ptr op)
  82. {    if ( dsp == dsbot + (min_dstack_size - 1) )
  83.         return_error(e_dictstackunderflow);
  84.     dsp--;
  85.     return 0;
  86. }
  87.  
  88. /* <key> <value> def - */
  89. /* We make this into a separate procedure because */
  90. /* the interpreter will almost always call it directly. */
  91. int
  92. zop_def(register os_ptr op)
  93. {    check_op(2);
  94.     if ( r_has_type(op - 1, t_null) )
  95.         return_error(e_typecheck);
  96.     check_dict_write(*dsp);
  97.     return dict_put(dsp, op - 1, op);
  98. }
  99. int
  100. zdef(os_ptr op)
  101. {    int code = zop_def(op);
  102.     if ( !code ) { pop(2); }
  103.     return code;
  104. }
  105.  
  106. /* <key> load <value> */
  107. int
  108. zload(register os_ptr op)
  109. {    ref *pvalue;
  110.     check_op(1);
  111.     if ( r_has_type(op, t_null) )
  112.         return_error(e_typecheck);
  113.     if ( r_has_type(op, t_name) )
  114.        {    /* Use the fast lookup */
  115.         if ( (pvalue = dict_find_name(op)) == 0 )
  116.             return_error(e_undefined);
  117.        }
  118.     else
  119.        {    if ( dict_lookup(dsbot, dsp, op, &pvalue) <= 0 )
  120.             return_error(e_undefined);
  121.        }
  122.     ref_assign(op, pvalue);
  123.     return 0;
  124. }
  125.  
  126. /* <key> <value> store - */
  127. int
  128. zstore(register os_ptr op)
  129. {    ref *pvalue;
  130.     int code;
  131.     check_op(2);
  132.     if ( r_has_type(op - 1, t_null) )
  133.         return_error(e_typecheck);
  134.     if ( dict_lookup(dsbot, dsp, op - 1, &pvalue) <= 0 )
  135.        {    code = dict_put(dsp, op - 1, op);
  136.         if ( code ) return code;
  137.        }
  138.     else
  139.         ref_assign_old(pvalue, op, "store");
  140.     pop(2);
  141.     return 0;
  142. }
  143.  
  144. /* get - implemented in zgeneric.c */
  145.  
  146. /* put - implemented in zgeneric.c */
  147.  
  148. /* <dict> <key> undef - */
  149. int
  150. zundef(register os_ptr op)
  151. {    check_type(op[-1], t_dictionary);
  152.     check_dict_write(op[-1]);
  153.     if ( !r_has_type(op, t_null) )
  154.         dict_undef(op - 1, op);    /* ignore undefined error */
  155.     pop(2);
  156.     return 0;
  157. }
  158.  
  159. /* <dict> <key> known <bool> */
  160. int
  161. zknown(register os_ptr op)
  162. {    register os_ptr op1 = op - 1;
  163.     ref *pvalue;
  164.     check_type(*op1, t_dictionary);
  165.     check_dict_read(*op1);
  166.     make_bool(op1,
  167.           (r_has_type(op, t_null) ? 0 :
  168.            dict_find(op1, op, &pvalue) > 0 ? 1 : 0));
  169.     pop(1);
  170.     return 0;
  171. }
  172.  
  173. /* <dict> <key> .knownget <value> true */
  174. /* <dict> <key> .knownget false */
  175. int
  176. zknownget(register os_ptr op)
  177. {    register os_ptr op1 = op - 1;
  178.     ref *pvalue;
  179.     check_type(*op1, t_dictionary);
  180.     check_dict_read(*op1);
  181.     if ( r_has_type(op, t_null) || dict_find(op1, op, &pvalue) <= 0 )
  182.     {    make_false(op1);
  183.         pop(1);
  184.     }
  185.     else
  186.     {    ref_assign(op1, pvalue);
  187.         make_true(op);
  188.     }
  189.     return 0;
  190. }
  191.  
  192. /* <key> where <dict> true */
  193. /* <key> where false */
  194. int
  195. zwhere(register os_ptr op)
  196. {    const ref *pdref = dsp;
  197.     ref *pvalue;
  198.     check_op(1);
  199.     if ( r_has_type(op, t_null) )
  200.        {    make_bool(op, 0);
  201.         return 0;
  202.        }
  203.     while ( 1 )
  204.        {    check_dict_read(*pdref);
  205.         if ( dict_find(pdref, op, &pvalue) > 0 ) break;
  206.         if ( --pdref < dsbot )
  207.            {    make_bool(op, 0);
  208.             return 0;
  209.            }
  210.        }
  211.     ref_assign(op, pdref);
  212.     push(1);
  213.     make_bool(op, 1);
  214.     return 0;
  215. }
  216.  
  217. /* copy for dictionaries -- called from zcopy in zgeneric.c. */
  218. /* Only the type of *op has been checked. */
  219. int
  220. zcopy_dict(register os_ptr op)
  221. {    os_ptr op1 = op - 1;
  222.     check_type(*op1, t_dictionary);
  223.     check_dict_read(*op1);
  224.     check_dict_write(*op);
  225.     if ( !dict_auto_expand && (dict_length(op) != 0 || dict_maxlength(op) < dict_length(op1)) )
  226.         return_error(e_rangecheck);
  227.     dict_copy(op1, op);
  228.     ref_assign(op - 1, op);
  229.     pop(1);
  230.     return 0;
  231. }
  232.  
  233. /* - currentdict <dict> */
  234. int
  235. zcurrentdict(register os_ptr op)
  236. {    push(1);
  237.     ref_assign(op, dsp);
  238.     return 0;
  239. }
  240.  
  241. /* - countdictstack <int> */
  242. int
  243. zcountdictstack(register os_ptr op)
  244. {    push(1);
  245.     make_int(op, dsp - dsbot + 1);
  246.     return 0;
  247. }
  248.  
  249. /* <array> dictstack <subarray> */
  250. int
  251. zdictstack(register os_ptr op)
  252. {    int depth = dsp - dsbot + 1;
  253.     int code;
  254.     check_write_type(*op, t_array);
  255.     if ( depth > r_size(op) )
  256.         return_error(e_rangecheck);
  257.     code = refcpy_to_old(op, 0, dsbot, depth, "dictstack");
  258.     if ( code < 0 )
  259.         return code;
  260.     r_set_size(op, depth);
  261.     return 0;
  262. }
  263.  
  264. /* - cleardictstack - */
  265. int
  266. zcleardictstack(os_ptr op)
  267. {    dsp = dsbot + (min_dstack_size - 1);
  268.     return 0;
  269. }
  270.  
  271. /* ------ Initialization procedure ------ */
  272.  
  273. op_def zdict_op_defs[] = {
  274.     {"0cleardictstack", zcleardictstack},
  275.     {"1begin", zbegin},
  276.     {"0countdictstack", zcountdictstack},
  277.     {"0currentdict", zcurrentdict},
  278.     {"2def", zdef},
  279.     {"1dict", zdict},
  280.     {"0dictstack", zdictstack},
  281.     {"0end", zend},
  282.     {"2known", zknown},
  283.     {"2.knownget", zknownget},
  284.     {"1load", zload},
  285.     {"1maxlength", zmaxlength},
  286.     {"2.setmaxlength", zsetmaxlength},
  287.     {"2store", zstore},
  288.     {"2undef", zundef},
  289.     {"1where", zwhere},
  290.     op_def_end(0)
  291. };
  292.